<?php

/*****************************运输管理*************************************/
class SystemAction extends CommAction{
    public function roleList(){
        $map['is_delete'] = 2;
        $info = M('admin_role')->where($map)->select();
        $this->assign('info',$info);
        $this->display();
    }

    //分配权限页面
    public function distributionRole(){
        $map['role_id'] = $_GET['role_id'];
        $my_menus = M('admin_role_menu')->where($map)->getField('menu_id',true);
        $this->assign('my_menus',$my_menus);
        $this->assign('role_id',$map['role_id']);
        $this->display();
    }

    //保存分配的权限
    public function save_role_menu(){
        $map['role_id'] = $_POST['role_id'];
        M('admin_role_menu')->where($map)->delete();
        $menu_id = $_POST['menu_id'];
        foreach($menu_id as $k=>$val){
            $data['menu_id'] = $val;
            $data['role_id'] = $map['role_id'];
            $exist[] = M('admin_role_menu')->add($data);
        }
        echo 1;
    }

    //角色新增/修改页面
    public function add_edit_role_index(){
        $map['id'] = $_GET['admin_role_id'];
        $info = M('admin_role')->where($map)->find();
        $this->assign('info',$info);
        $this->display();
    }
    //新增角色
    public function add_role(){
        $map['name'] = $_POST['role_name'];
        $exist = M('admin_role')->where($map)->find();
        if($exist){
            $info['value'] = 1;
            $info['msg'] = '此数据已存在,请勿重复添加!';
            echo json_encode($info);
        }else{
            $data['name'] = $_POST['role_name'];
            $data['description'] = $_POST['description'];
            $data['create_time'] = time();
            $add = M('admin_role')->add($data);
            if($add){
                $info['value'] = 2;
                $info['msg'] = '新增成功!';
                echo json_encode($info);
            }else{
                $info['value'] = 3;
                $info['msg'] = '新增失败!';
                echo json_encode($info);
            }
        }
    }

    public function edit_role_show(){
        $map['id'] = $_POST['admin_role_id'][0];
        $info = M('admin_role')->where($map)->find();
        echo json_encode($info);
    }
    //修改角色
    public function edit_role(){
        $map['id'] = $_POST['admin_role_id'];
        $data['name'] = $_POST['role_name'];
        $data['description'] = $_POST['description'];
        $save = M('admin_role')->where($map)->save($data);
        if($save){
            $info['value'] = 2;
            $info['msg'] = '修改成功!';
            echo json_encode($info);
        }else{
            $info['value'] = 3;
            $info['msg'] = '修改失败!';
            echo json_encode($info);
        }
        
    }

    //删除
    public function del_role(){
        $ids = $_POST['ids'];
        foreach($ids as $k=>$val){
            $map['id'] = $val;
            M('admin_role')->where($map)->delete();
        }
        echo 1;
    }

    //用户列表
    public function userList(){
        $map['au.is_delete'] = 2;
        $map['au.status'] = 1;
        $info = M('admin_user as au')
              ->join('jl_admin_user_role as aur on au.id=aur.user_id')
              ->join('jl_admin_role as ar on aur.role_id=ar.id')
              ->order('au.lastlogin desc')
              ->field('au.id,au.truename,au.mobile,au.name,au.password,au.creater,au.lastlogin,au.createtime,ar.name as role_name')
              ->where($map)
              ->select();
        $this->assign('info',$info);
        $this->display();
    }

    //新增/修改用户页面
    public function add_edit_user_index(){
        $roles = M('admin_role')->select();
        $this->assign('roles',$roles);
        $map['id'] = $_GET['admin_id'];
        $info = M('admin_user')->where($map)->find();
        $info['role_id'] = M('admin_user_role')->where(array('user_id'=>$_GET['admin_id']))->getField('role_id');
        $this->assign('info',$info);
        $this->display();
    }

    //新增用户
    public function add_admin(){
        $map['name'] = $_POST['name'];
        $data['truename'] = $_POST['truename'];
        $data['mobile'] = $_POST['mobile'];
        $data['name'] = $_POST['name'];
        $data['password'] = $_POST['password'];
        $exist = M('admin_user')->where($map)->find();
        if($exist){
            $info['value'] = 1;
            $info['msg'] = "此账号已存在!";
            echo json_encode($info);
        }else{
            $data['creater'] = M('admin_user')->where(array('id'=>getLoginUid()))->getField('truename');
            $data['createtime'] = time();
            $add = M('admin_user')->add($data);
            if($add){
                $data1['user_id'] = M('admin_user')->where(array('name'=>$_POST['name']))->getField('id');
                $data1['role_id'] = $_POST['role_id'];
                M('admin_user_role')->add($data1);
                $info['value'] = 2;
                $info['msg'] = "新增成功!";
                echo json_encode($info);
            }else{
                $info['value'] = 3;
                $info['msg'] = "新增失败!";
                echo json_encode($info);
            }
            
        }
    }

    //修改用户信息
    public function edit_admin(){
        $map['id'] = $_POST['admin_id'];
        $data['truename'] = $_POST['truename'];
        $data['mobile'] = $_POST['mobile'];
        $data['name'] = $_POST['name'];
        $data['password'] = $_POST['password'];
        $data['creater'] = M('admin_user')->where(array('id'=>getLoginUid()))->getField('truename');
        $save = M('admin_user')->where($map)->save($data);
        if($save){
            $map1['user_id'] = $_POST['admin_id'];
            $data1['role_id'] = $_POST['role_id'];
            M('admin_user_role')->where($map1)->save($data1);
            $info['value'] = 2;
            $info['msg'] = "修改成功!";
            echo json_encode($info);
        }else{
            $info['value'] = 3;
            $info['msg'] = "修改失败!";
            echo json_encode($info);
        }
            
    }
    //返回用户信息
    public function return_admin_user_info(){
        $admin_id = $_POST['admin_id'][0];
        echo json_encode($admin_id);
    }

    //删除
    public function del_user(){
        $ids = $_POST['ids'];
        foreach($ids as $k=>$val){
            $map['id'] = $val;
            $data['is_delete'] = 1;
            $data['status'] = 2;
            M('admin_user')->where($map)->save($data);
        }
        echo 1;
    }

    //数据采集
    public function collection(){
        // $url = 'http://www.news.cn/local/index.htm';
        // $ch = curl_init();
        // $timeout = 50;
        // curl_setopt ($ch, CURLOPT_URL, $url);
        // curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        // $file_contents = curl_exec($ch);
        // curl_close($ch);
        // // echo $file_contents;
        // $pattern = '/<div class="news-item">(.*)<\/div>/';

        // preg_match_all($pattern, $file_contents, $matches);
        // dump($matches);
        $this->display();
    }


}
